package com.xc.erp.modules.erp.controller.function;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.constants.ExceptionConstants;
import com.xc.erp.common.utils.SecurityUtils;
import com.xc.erp.modules.erp.domain.AjaxResult;
import com.xc.erp.modules.erp.domain.pojo.JshFunction;
import com.xc.erp.modules.erp.service.JshFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = {"功能管理"})
@RestController
@RequestMapping("/function")
public class FunctionController {
    @Autowired
    private JshFunctionService jshFunctionService;

    @ApiOperation("查询功能列表")
    @GetMapping("list/{pageSize}/{pageNum}")
    public AjaxResult list(@PathVariable Integer pageSize,
                           @PathVariable Integer pageNum,String name){
        Page<JshFunction> list = jshFunctionService.list(pageSize, pageNum, name);
        return AjaxResult.success(list);
    }

    @Log(modul = "功能管理",desc = "新增")
    @ApiOperation("新增功能")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody JshFunction jshFunction){
         jshFunction.setEnabled(true);
        jshFunctionService.save(jshFunction);
        return AjaxResult.success();
    }

    @ApiOperation("检查名称是否存在")
    @GetMapping("/checkIsNameExist")
    public AjaxResult checkIsNameExist(Long id,String name){
        boolean status = jshFunctionService.checkIsNameExist(id,name);
        return AjaxResult.success().put("status",status);
    }

    @ApiOperation("检查编号是否存在")
    @GetMapping("/checkIsNumberExist")
    public AjaxResult checkIsNumberExist(Long id,String number){
        boolean status = jshFunctionService.checkIsNumberExist(id,number);
        return AjaxResult.success().put("status",status);
    }

    @Log(modul = "功能管理",desc = "编辑")
    @ApiOperation("编辑功能")
    @PutMapping("update")
    public AjaxResult update(@RequestBody JshFunction jshFunction){
        jshFunctionService.updateById(jshFunction);
        return AjaxResult.success();
    }

    @Log(modul = "功能管理",desc = "删除")
    @ApiOperation("删除功能")
    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        jshFunctionService.removeById(id);
        return AjaxResult.success();
    }

    @Log(modul = "功能管理",desc = "批量删除")
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody Long[] ids){
        jshFunctionService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @Log(modul = "功能管理",desc = "批量启用/禁用")
    @ApiOperation("批量修改状态")
    @PutMapping("batchSetStatus/{status}")
    public AjaxResult batchSetStatus(@PathVariable boolean status,@RequestBody Long[] ids){
        jshFunctionService.batchSetStatus(status,ids);
        return AjaxResult.success();
    }


    @ApiOperation("查询所有菜单列表")
    @GetMapping("functionList")
    public AjaxResult functionList(){
        List<JshFunction> list =  jshFunctionService.listFunction();
        return AjaxResult.success(jshFunctionService.buildMenus(list));
    }

    @ApiOperation("查询当前角色所有的权限")
    @GetMapping("selectFunctionByRole/{roleId}")
    public AjaxResult selectFunction(@PathVariable Integer roleId){
        List<JshFunction> functionList = jshFunctionService.selectFunctionRole(roleId);
        return AjaxResult.success(jshFunctionService.buildMenus(functionList));
    }
}
